<?php declare(strict_types=1);


namespace App\Services\Refund\Strategy;

use App\Api\Controllers\Config\LeshuaUnionPayConfigController;
use App\Api\Controllers\LeshuaUnion\BaseController;
use App\Api\Controllers\LeshuaUnion\PayController;
use App\Exceptions\BizException;
use App\Models\Store;
use App\Services\Pay\ResultVo;

class LeshuaunionRefundStrategy implements RefundStrategyInterface
{

    public function refundQuery(array $data): array
    {
        $store = Store::query()->where("store_id", $data["store_id"])->first();

        if (empty($store)) {
            throw new BizException("门店不存在");
        }
        $configService = new LeshuaUnionPayConfigController();
        $merchant = $configService->merchant($store->store_id, $store->pid);
        $config = $configService->config($store->config_id);
        $pay = new PayController();
        $result = $pay->refund_query(
            [
                "mch_id" => $merchant->mch_id,
                "out_trade_no" => $data["out_trade_no"],
                "refund_no" => $data["refund_no"]
            ],
            $config
        );
        $status = -1;
        $other_refund_no = $refund_detail = $refund_time = "";
        if ($result["status"] == 1) {
            $status = 1;
            $other_refund_no = $result["data"]["leshua_refund_id"];
            $refund_detail = $result["data"]["refund_detail"];
            $refund_time = empty($result["data"]["refund_time"]) ? date("Y-m-d H:i:s", time()) : date("Y-m-d H:i:s", strtotime($result["data"]["refund_time"]));
        } else if ($result["status"] == 0) {
            $status = 0;
        } else if ($result["status"] == -1) {
            $status = -1;
        }

        return [
            "status" => $status,
            "other_refund_no" => $other_refund_no,
            "refund_detail" => $refund_detail,
            "refund_time" => $refund_time
        ];
    }
}

